//
//  PickUpAddressViewController.m
//  Hui
//
//  Created by HawkingWang on 16/10/27.
//  Copyright © 2016年 Hui. All rights reserved.
//

#import "PickUpAddressViewController.h"
#import "LocationViewController.h"//地图Controller

@interface PickUpAddressViewController () <UITextFieldDelegate, UITextViewDelegate>
/**取货人姓名*/
@property (weak, nonatomic) IBOutlet UITextField *name;
/**选择性别*/
@property (weak, nonatomic) IBOutlet UIImageView *maleImage;
@property (weak, nonatomic) IBOutlet UIImageView *femaleImage;
/**取货人电话*/
@property (weak, nonatomic) IBOutlet UITextField *phone;
/**取货地址*/
@property (weak, nonatomic) IBOutlet UILabel *address;
/**详细地址*/
@property (weak, nonatomic) IBOutlet UITextView *detailAddress;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UIView *inputView;

@end

@implementation PickUpAddressViewController
{
    NSString *_sex;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.navigationController.navigationBar.hidden = YES;
    _detailAddress.delegate = self;
    _name.delegate = self;
    
    
    //键盘监听
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}
#pragma mark === 性别选择 ===
- (IBAction)chooseMale:(id)sender {
    _sex = @"0";
    _maleImage.image = [UIImage imageNamed:@"right-touch"];
    _femaleImage.image = [UIImage imageNamed:@"right"];
}
- (IBAction)chooseFemale:(id)sender {
    _sex = @"1";
    _maleImage.image = [UIImage imageNamed:@"right"];
    _femaleImage.image = [UIImage imageNamed:@"right-touch"];
}
#pragma mark === 跳转到地图界面 ===
- (IBAction)jumoToMap:(id)sender {
    LocationViewController *location = [[LocationViewController alloc] init];
    [self.navigationController pushViewController:location animated:YES];
}

#pragma mark === 提交 ===
- (IBAction)commit:(id)sender {
    if ([_name.text isEqualToString:@""]) {
        [EBUtility prompt:@"请输入联系人姓名" with:self];
        return;
    }
    if ([_phone.text isEqualToString:@""]) {
        [EBUtility prompt:@"请输入联系方式" with:self];
        return;
    }
    if (![EBUtility isMobileNumber:_phone.text]) {
        [EBUtility prompt:@"请输入正确的联系方式" with:self];
    }
    if ([_detailAddress.text isEqualToString:@""]) {
        [EBUtility prompt:@"请输入详细地址" with:self];
        return;
    }
}

#pragma mark === UITextFieldDelegate ===
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if (textView == _detailAddress) {
        _detailLabel.hidden = YES;
    }
    return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    if ([_detailAddress.text isEqualToString:@""]) {
        _detailLabel.hidden = NO;
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_name resignFirstResponder];
    [_phone resignFirstResponder];
    [_address resignFirstResponder];
    [_detailAddress resignFirstResponder];
}
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 键盘显示的监听方法
-(void) keyboardWillShow:(NSNotification *) note
{
    if ([_detailAddress isFirstResponder]) {
        // 获取键盘的位置和大小
        CGRect keyboardBounds;
        [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
        NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
        NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
        
        // Need to translate the bounds to account for rotation.
        keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
        
        // 获取输入框的位置和大小
        CGRect containerFrame = _detailAddress.frame;
        // 计算出输入框的y坐标
        if (SCREEN_HEIGHT - keyboardBounds.size.height < containerFrame.origin.y + containerFrame.size.height) {
            CGFloat y = (keyboardBounds.size.height + 165) - (SCREEN_HEIGHT - containerFrame.origin.y);
            // 动画改变位置
            [UIView animateWithDuration:[duration doubleValue] animations:^{
                [UIView setAnimationBeginsFromCurrentState:YES];
                [UIView setAnimationDuration:[duration doubleValue]];
                [UIView setAnimationCurve:[curve intValue]];
                // 更改输入框的位置
                _inputView.frame = CGRectMake(0, 79-y, SCREEN_WIDTH, 323.5);
            }];
        }
    }
}

#pragma mark === 键盘隐藏的监听方法 ===
-(void) keyboardWillHide:(NSNotification *) note
{
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    // 动画改变位置
    [UIView animateWithDuration:[duration doubleValue] animations:^{
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:[duration doubleValue]];
        [UIView setAnimationCurve:[curve intValue]];
        // 更改输入框的位置
        _inputView.frame = CGRectMake(0, 79, SCREEN_WIDTH, 323.5);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
